package com.dotstone.chipism.util;

import android.text.format.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class TimeUtil {

	public static SimpleDateFormat sf = null;

	/**
	 * 获取当前时间
	 *
	 * @return
	 */
	public String getNowTime() {
		String timeString = null;
		Time time = new Time();
		time.setToNow();
		String year = thanTen(time.year);
		String month = thanTen(time.month + 1);
		String monthDay = thanTen(time.monthDay);
		String hour = thanTen(time.hour);
		String minute = thanTen(time.minute);

		timeString = year + "-" + month + "-" + monthDay + " " + hour + ":"
				+ minute;
		// System.out.println("-------timeString----------" + timeString);
		return timeString;
	}

	public int calculate(int year, int month) {

		boolean yearleap = judge(year);
		int day;
		if (yearleap && month == 2) {
			day = 29;
		} else if (!yearleap && month == 2) {
			day = 28;
		} else if (month == 4 || month == 6 || month == 9 || month == 11) {
			day = 30;
		} else {
			day = 31;
		}
		return day;
	}

	public boolean judge(int year) {
		boolean yearleap = (year % 400 == 0) || (year % 4 == 0)
				&& (year % 100 != 0);// 采用布尔数据计算判断是否能整除
		return yearleap;
	}

	/**
	 * 十一下加零
	 *
	 * @param str
	 * @return
	 */
	public String thanTen(int str) {

		String string = null;

		if (str < 10) {
			string = "0" + str;
		} else {

			string = "" + str;

		}
		return string;
	}

	/**
	 * 计算时间差
	 *
	 * @param starTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @param type
	 *            返回类型 ==1----天，时，分。 ==2----时
	 * @return 返回时间差
	 */
	public static String getTimeDifference(String starTime, String endTime) {
		String timeString = "";
		SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");

		try {
			Date parse = dateFormat.parse(starTime);
			Date parse1 = dateFormat.parse(endTime);

			long diff = parse1.getTime() - parse.getTime();

			long day = diff / (24 * 60 * 60 * 1000);
			long hour = (diff / (60 * 60 * 1000) - day * 24);
			long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
			long s = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
			long ms = (diff - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
					- min * 60 * 1000 - s * 1000);
			// System.out.println(day + "天" + hour + "小时" + min + "分" + s +
			// "秒");
			long hour1 = diff / (60 * 60 * 1000);
			String hourString = hour1 + "";
			long min1 = ((diff / (60 * 1000)) - hour1 * 60);
		//	timeString = hour1 + "小时" + min1 + "分";
			timeString = hour1 + ";" + min1;
			// System.out.println(day + "天" + hour + "小时" + min + "分" + s +
			// "秒");

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return timeString;

	}

	/**
	 * 计算相差的小时
	 *
	 * @param starTime
	 * @param endTime
	 * @return
	 */
	public String getTimeDifferenceHour(String starTime, String endTime) {
		String timeString = "";
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		try {
			Date parse = dateFormat.parse(starTime);
			Date parse1 = dateFormat.parse(endTime);

			long diff = parse1.getTime() - parse.getTime();
			String string = Long.toString(diff);

			float parseFloat = Float.parseFloat(string);

			float hour1 = parseFloat / (60 * 60 * 1000);

			timeString = Float.toString(hour1);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return timeString;

	}

	/**
	 * 获取时间中的某一个时间点
	 *
	 * @param str
	 * @param type
	 * @return
	 */
	public String getJsonParseShiJian(String str, int type) {
		String shijanString = null;
		String nian = str.substring(0, str.indexOf("-"));
		String yue = str.substring(str.indexOf("-") + 1, str.lastIndexOf("-"));
		String tian = str.substring(str.lastIndexOf("-") + 1, str.indexOf(" "));
		String shi = str.substring(str.indexOf(" ") + 1, str.lastIndexOf(":"));
		String fen = str.substring(str.lastIndexOf(":") + 1, str.length());
		switch (type) {
			case 1:
				shijanString = nian;
				break;
			case 2:
				shijanString = yue;
				break;
			case 3:
				shijanString = tian;
				break;
			case 4:
				shijanString = shi;
				break;
			case 5:
				shijanString = fen;
				break;
		}
		return shijanString;
	}

	/**
	 * Sring变int
	 *
	 * @param str
	 * @return
	 */
	public int strToInt(String str) {
		int value = 0;
		value = Integer.parseInt(str);
		return value;
	}

	/**
	 * 与当前时间比较早晚
	 *
	 * @param time
	 *            需要比较的时间
	 * @return 输入的时间比现在时间晚则返回true
	 */
	public boolean compareNowTime(String time) {
		boolean isDayu = false;

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		try {
			Date parse = dateFormat.parse(time);
			Date parse1 = dateFormat.parse(getNowTime());

			long diff = parse1.getTime() - parse.getTime();
			if (diff <= 0) {
				isDayu = true;
			} else {
				isDayu = false;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return isDayu;
	}

	/**
	 * 把时间戳变yyyy-MM-dd HH:mm格式时间
	 *
	 * @param time
	 * @return
	 */
	public static  String getDateToString(long time) {
		Date d = new Date(time);
		sf = new SimpleDateFormat("HH:mm");
		return sf.format(d);
	}

	/**
	 * 返回时间戳
	 *
	 * @param time
	 * @return
	 */
	public long dataOne(String time) {
		SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm",
				Locale.CHINA);
		Date date;
		long l = 0;
		try {
			date = sdr.parse(time);
			l = date.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return l;
	}

	/**
	 * 比较两个时间
	 *
	 * @param starTime
	 *            开始时间
	 * @param endString
	 *            结束时间
	 * @return 结束时间大于开始时间返回true，否则反之֮
	 */
	public boolean compareTwoTime(String starTime, String endString) {
		boolean isDayu = false;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

		try {
			Date parse = dateFormat.parse(starTime);
			Date parse1 = dateFormat.parse(endString);

			long diff = parse1.getTime() - parse.getTime();
			if (diff >= 0) {
				isDayu = true;
			} else {
				isDayu = false;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return isDayu;

	}

	public boolean compareTwoTime2(String starTime, String endString) {
		boolean isDayu = false;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		try {
			Date parse = dateFormat.parse(starTime);
			Date parse1 = dateFormat.parse(endString);

			long diff = parse1.getTime() - parse.getTime();
			if (diff >= 0) {
				isDayu = true;
			} else {
				isDayu = false;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return isDayu;

	}

	/**
	 * 获取年
	 *
	 * @param time
	 * @return
	 */
	public String getTimeYear(String time) {

		String substring = time.substring(0, time.lastIndexOf(" "));
		return substring;

	}

	/**
	 * 换算小时，0.5小时-->0小时30分
	 *
	 * @param hour
	 * @return
	 */
	private String convertTime(String hour) {

		String substring = hour.substring(0, hour.lastIndexOf("."));
		String substring2 = hour.substring(hour.lastIndexOf(".") + 1,
				hour.length());
		substring2 = "0." + substring2;
		float f2 = Float.parseFloat(substring2);
		f2 = f2 * 60;
		String string = Float.toString(f2);
		String min = string.substring(0, string.lastIndexOf("."));
		return substring + "小时" + min + "分";

	}
}

